package com.example.it.studypulltoreference;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Created by 鲁迅认识的那只猹 on 7/27/2017 9:02 PM.
 * Class Desc:
 * <p>
 * Update
 * Updated by IT on 7/27/2017 9:02 PM
 * Desc:
 */

public class DataAdapter extends BaseAdapter {

    private Context context;
    private List<String> dataSource;

    public DataAdapter(Context context, List<String> dataSource) {
        this.context = context;
        this.dataSource = dataSource;
    }

    @Override
    public int getCount() {
        return this.dataSource.size();
    }

    @Override
    public Object getItem(int position) {
        return this.dataSource.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            LayoutInflater inflater = LayoutInflater.from(this.context);
            view = inflater.inflate(android.R.layout.simple_list_item_activated_1, null);
        }
        TextView textView = (TextView) view.findViewById(android.R.id.text1);
        textView.setText(this.dataSource.get(position));
        return view;
    }

    /*
    * 刷新时通过此方法向数据源中添加数据
    * */
    public void addItem(String item) {
        this.dataSource.add(item);
    }
}
